public class HashBuck1<K,V> {
    public static class Node<K,V>{
        public K key;
        public V val;
        public Node<K,V> next;
        public Node(K key,V val){
            this.key=key;
            this.val=val;
        }
    }
    Node<K,V>[] array=(Node<K,V>[])new Node[10];
    public int useSize;
    public static final float LOAD_FACTOR=0.75f;
    public void put(K key,V val){
        int index= key.hashCode()/array.length;

        Node<K,V> cur=array[index];
        while(cur!=null){
            //注意key是K引用类型的，不能用"=="比较
            if(cur.key.equals(key)){
                cur.val=val;
                return;
            }
            cur=cur.next;
        }
        //不相同则头插法
        Node<K,V> newNode=new Node<>(key,val);
        newNode.next=array[index];
        array[index]=newNode;

        useSize++;

        //如果超过负载因子(0.75)就要扩容
        if(doLoadFactor()>=LOAD_FACTOR){
            resize();
        }

    }
    private float doLoadFactor(){
        //扩容
        return useSize*1.0f/array.length;
    }

    public void resize(){
        Node<K,V>[] newNode=new Node[array.length*2];
        for(int i=0;i<array.length;i++){
            Node<K,V> cur=array[i];
            while(cur!=null){
                int newIndex=cur.key.hashCode()% newNode.length;
                Node<K,V> curN=cur.next;
                cur.next=array[newIndex];
                array[newIndex]=cur;
                cur=curN;
            }
        }
        array=newNode;
    }

}
